/*
 * @Author: liuying 247220246@qq.com
 * @Date: 2025-04-11 15:43:21
 * @LastEditors: liuying 247220246@qq.com
 * @LastEditTime: 2025-04-21 18:12:09
 * @FilePath: \my-book-react\src\pages\search\stores.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { combineReducers, createAction, ActionCreatorWithPayload } from '@reduxjs/toolkit';
import { parse } from 'query-string';
import { createTempSlice } from '@/stores';
interface IRankingAction {
  setTabKey: ActionCreatorWithPayload<string, string>;
}
interface ISearchAction {
  setSearchMode: ActionCreatorWithPayload<boolean, string>;
  setSearchKeyword: ActionCreatorWithPayload<string, string>;
}
export const rankingAction: IRankingAction = {
  setTabKey: createAction('INIT'),
};
export const searchActions: ISearchAction = {
  setSearchMode: createAction('INIT'),
  setSearchKeyword: createAction('INIT'),
};
// 导出一个函数createReducer，用于创建一个reducer
export const createReducer = (key: string) => {
  // 获取url中的keyword参数
  const keyword = parse(window.location.search).keyword as string;
  // 创建一个临时slice，用于存储searchParams和keyword
  const { set: setSearchKeyword, reducer: searchKeyword } = createTempSlice<string>(
    'searchParams',
    typeof keyword !== 'string' ? '' : keyword,
    key
  );

  // 创建一个临时slice，用于存储searchMode
  const { set: setSearchMode, reducer: searchMode } = createTempSlice<boolean>('searchMode', false, key);

  // 将setSearchMode和setSearchKeyword赋值给searchActions
  searchActions.setSearchMode = setSearchMode;
  searchActions.setSearchKeyword = setSearchKeyword;
  // 返回一个reducer对象，其中包含searchKeyword和searchMode
  return {
    reducers: {
      [key]: combineReducers({
        searchKeyword,
        searchMode,
      }),
    },
  };
};
